package flc.ast.fragment;

import android.Manifest;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.weather.lib.WeatherManager;

import flc.ast.R;
import flc.ast.activity.DebugActivity;
import flc.ast.databinding.FragmentDebugBinding;
import flc.ast.utils.Content;
import stark.common.apis.base.Weather;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.LocationUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DebugFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DebugFragment extends BaseNoModelFragment<FragmentDebugBinding> {

    private boolean isLocation = false;//是否获取定位
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    isLocation = false;
                    break;
                case 2:
                    isLocation = true;
                    dismissDialog();
                    ToastUtils.showShort(R.string.get_location_suc);
                    stopLoc();
                    break;
            }
            return false;
        }
    });

    private String city = "";
    private String weather = "";


    @Override
    protected int onCreate() {
        return R.layout.fragment_debug;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rlTcpServer.setOnClickListener(this);
        mDataBinding.rlTcpClient.setOnClickListener(this);
        mDataBinding.rlUdpServer.setOnClickListener(this);
        mDataBinding.rlUdpClient.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlTcpServer://tcp服务端
                DebugActivity.type = 0;
                startActivity(DebugActivity.class);
                break;
            case R.id.rlTcpClient://tcp客户端
                DebugActivity.type = 1;
                startActivity(DebugActivity.class);
                break;
            case R.id.rlUdpServer://udp服务端
                DebugActivity.type = 2;
                startActivity(DebugActivity.class);
                break;
            case R.id.rlUdpClient://udp客户端
                DebugActivity.type = 3;
                startActivity(DebugActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
//        if (isLocation) return;
//        getWeatherByMap();
    }

    //获取权限
    private void getWeatherByMap() {
        StkPermissionHelper.permission(Manifest.permission.ACCESS_FINE_LOCATION)
                .reqPermissionDesc(getString(R.string.get_location_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startLoc();
                    }
                })
                .request();
    }

    //开始定位
    private AMapLocationClient client;

    private void startLoc() {
        if (client != null) {
            return;
        }
        showDialog(getString(R.string.get_location_ing));
        client = new AMapLocationClient(mContext);
        AMapLocationClientOption clientOption = new AMapLocationClientOption();
        clientOption.setInterval(3000);
        clientOption.setNeedAddress(true);
        client.setLocationOption(clientOption);
        client.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation == null) return;
                int errCode = aMapLocation.getErrorCode();
                if (errCode != 0) return;
                //获取位置信息
                city = aMapLocation.getCity();
                //根据城市名称来获取天气信息
                getWeather(aMapLocation.getCity());
                mDataBinding.tvCity.setText(city);
                if (TextUtils.isEmpty(city)) {
                    handler.sendEmptyMessage(1);//还未获取到定位信息
                } else {
                    handler.sendEmptyMessage(2);//已获取定位信息
                }
            }
        });
        client.startLocation();
    }

    //停止获取定位
    private void stopLoc() {
        if (client != null) {
            client.stopLocation();
            client.onDestroy();
            client = null;
        }
    }

    //获取天气
    private void getWeather(String city) {
        WeatherManager.getWeather(this, city, new IReqRetCallback<Weather>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Weather result) {
                if (success && result != null) {
                    weather = result.future.get(0).weather;//获取当天天气
                    mDataBinding.tvWeather.setText(weather);
                    int index = result.realtime.temperature.lastIndexOf('/');
                    if (index != -1) {
                        mDataBinding.tvTemperature.setText(result.realtime.temperature.substring(0, index));
                    } else {
                        mDataBinding.tvTemperature.setText(result.realtime.temperature);
                    }
                    mDataBinding.tvHumidness.setText(getString(R.string.humidness) + result.realtime.humidity);
                    mDataBinding.tvWindSpeed.setText(getString(R.string.wind_speed) + result.realtime.direct + result.realtime.power);
                    if (weather.contains("晴")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a1);
                    } else if (weather.contains("多云")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a2);
                    } else if (weather.contains("阴")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a3);
                    } else if (weather.contains("小雨")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a4);
                    } else if (weather.contains("中雨")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a5);
                    } else if (weather.contains("大雨")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a6);
                    } else if (weather.contains("雷")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a7);
                    } else if (weather.contains("雪")) {
                        mDataBinding.ivWeather.setImageResource(R.drawable.a8);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopLoc();
    }
}